<template>
<div class="projects">
    <query-box :queryForm="queryForm" @search="handleSearch(true)"></query-box>
    <table-list :tableData="tableData" @flash="handleSearch" @add="resetForm"></table-list>
    <add-dialog :addForm="addForm" @save="handleSave"></add-dialog>
</div>
</template>

<script>
import AddDialog from './AddDialog'
import TableList from './TableList'
import QueryBox from './QueryBox'
export default {
    name: 'ProjectsIndex',
    components: {
        AddDialog,
        QueryBox,
        TableList
    },
    data () {
        return {
            tableData: {
                total: 0,
                pageSize: 10,
                currentPage: 1,
                loading: false,
                isSearch: false,
                rows: []
            },
            addForm: {
                show: false,
                title: '新增',
                form: {
                    projectName: null,
                    projectCode: null,
                    projectDesc: null
                }
            },
            queryForm: {
                name: null
            }
        }
    },
    created () {
        this.init()
    },
    methods: {
        handleSave () {
            this.$post('/project/add', this.addForm.form).then(res => {
                if (res) {
                    this.$message.success('保存成功')
                    this.addForm.show = false
                    this.init()
                }
            })
        },
        handleSearch (flag) {
            if (flag) {
                this.tableData.currentPage = 1
            }
            this.tableData.isSearch = true
            this.init()
        },
        init () {
            const {
                name
            } = this.queryForm
            const {
                pageSize,
                currentPage
            } = this.tableData
            const params = {
                keyword: name,
                pageSize: pageSize,
                pageNum: currentPage
            }
            this.tableData.loading = true
            this.$post('/project/list', params).then(res => {
                if (res) {
                    const { rows, total } = res
                    this.tableData.rows = rows
                    this.tableData.total = total
                }
            }).finally(() => {
                this.tableData.loading = false
            })
        },
        resetForm () {
            this.addForm = {
                show: true,
                title: '新增',
                form: {
                    projectName: null,
                    projectCode: null,
                    projectDesc: null
                }
            }
        }
    }
}
</script>
